#ifndef QSWITCHBUTTON_H
#define QSWITCHBUTTON_H
#include <QTimer>
#include <QtWidgets/QWidget>

#define SLIPER_TIME_MS			(2)
#define	SLIPER_MOVE_STEP		(2)
#define SLIPER_BLOCK_MARGIN		(1)
#define SLIPER_TRACE_MARGIN     (3)
#define SLIPER_BLOCK_LEN        (20)
#define SLIPER_BLOCK_X			(SLIPER_BLOCK_MARGIN + SLIPER_TRACE_MARGIN)
#define SLIPER_BLOCK_Y			(SLIPER_BLOCK_MARGIN + SLIPER_TRACE_MARGIN)
#define SLIPER_BLOCK_INTER_R    (4)

class SwitchControl : public QWidget
{
	Q_OBJECT

public:
	explicit SwitchControl(QWidget *parent = 0);
	bool isToggled() const;
	void setBackgroundColor(QColor color);
	void setCheckedColor(QColor color);
	void setDisbaledColor(QColor color);

public slots:
	void setToggle(bool sw);

protected:
	void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
	void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
	void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
	void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
	QSize sizeHint() const Q_DECL_OVERRIDE;
	QSize minimumSizeHint() const Q_DECL_OVERRIDE;

signals:
	void toggled(bool checked);

private slots:
	void onTimeout();

private:
	bool m_bChecked;
	QColor m_checkedColor;
	QColor m_disabledColor;
	qreal m_nX;
	qreal m_nY;
	qint16 m_nHeight;
	qint16 m_nMargin;
	QTimer m_timer;
	qreal m_nX_min;
	qreal m_nX_max;
	qreal m_block_len;
};

#endif // QSWITCHBUTTON_H
